﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Globalization;


namespace StandardFramework      
{
    [Serializable]            
    public class BaseComponent
    {
        protected static int NumberOfComponents = 0;

        private Guid _guid;

        public Guid GUID
        {
            get { return _guid; }
        }

        private string _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }


        protected Dictionary<Type, BaseComponent> _mapComponents = new Dictionary<Type, BaseComponent>();

        public Dictionary<Type, BaseComponent> Components
        {
            get { return _mapComponents; }
            set { _mapComponents = value; }
        }

        protected List<BaseComponent> _listComponents = new List<BaseComponent>();

        public List<BaseComponent> ListComponents
        {
            get { return _listComponents; }
            set { _listComponents = value; }
        }

        public BaseComponent()
        {
            _guid = Guid.NewGuid();
            _name = "Component";
            NumberOfComponents++;
        }

        ~BaseComponent()
        {
            NumberOfComponents--;
        }

        public virtual void Update(float dtime) { }

        public void AddComponent(BaseComponent component)
        {
            if (!_mapComponents.ContainsKey(component.GetType()))
            {
                _mapComponents.Add(component.GetType(), component);
                _listComponents.Add(component);
            }
        }

        public BaseComponent GetComponent(Type type)
        {
            BaseComponent result = null;

            if (_mapComponents.ContainsKey(type))
            {
                result = _mapComponents[type];
            }

            return result;
        }


        public List<BaseComponent> GetComponents(Type type)
        {
            List<BaseComponent> result = new List<BaseComponent>();

            foreach (BaseComponent component in _mapComponents.Values)
            {
                if ((typeof(BaseComponent)).IsAssignableFrom(type))
                {
                    result.Add(component);
                }
            }

            return result;
        }
    }
}
